package com.gridmobi.xrtb.domain.dsp.common;

import com.gridmobi.xrtb.domain.dsp.common.enums.QuickStartTimeE;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.Calendar;
import java.util.Date;

/**
 * 处理快捷日期的工具类
 * @author huangyongchao
 */
public class QuickTimeUtil {

    private Date startTime;
    private Date endTime;
    private String startTimeStr;
    private String endTimeStr;

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public String getStartTimeStr() {
        return startTimeStr;
    }

    public void setStartTimeStr(String startTimeStr) {
        this.startTimeStr = startTimeStr;
    }

    public String getEndTimeStr() {
        return endTimeStr;
    }

    public void setEndTimeStr(String endTimeStr) {
        this.endTimeStr = endTimeStr;
    }

    public static QuickTimeUtil getByQuickTime(Integer quickTime) {
        QuickTimeUtil dto = new QuickTimeUtil();
        if (QuickStartTimeE.Today.v.equals(quickTime)) {
            Date end = new Date();
            dto.setEndTime(end);
            dto.setStartTime(firstDate(0));
            dto.setStartTimeStr(DateFormatUtils.format(dto.getStartTime(),Patten.DATETIME));
            dto.setEndTimeStr(DateFormatUtils.format(dto.getEndTime(),Patten.DATETIME));


        } else if (QuickStartTimeE.Yesterday.v.equals(quickTime)) {


            dto.setEndTime(lastDate(-1));
            dto.setStartTime(firstDate(-1));
            dto.setStartTimeStr(DateFormatUtils.format(dto.getStartTime(),Patten.DATETIME));
            dto.setEndTimeStr(DateFormatUtils.format(dto.getEndTime(),Patten.DATETIME));


        } else if (QuickStartTimeE.Last3Days.v.equals(quickTime)) {

            dto.setEndTime(lastDate(-1));
            dto.setStartTime(firstDate(-3));
            dto.setStartTimeStr(DateFormatUtils.format(dto.getStartTime(),Patten.DATETIME));
            dto.setEndTimeStr(DateFormatUtils.format(dto.getEndTime(),Patten.DATETIME));



        } else if (QuickStartTimeE.Last7Days.v.equals(quickTime)) {
            dto.setEndTime(lastDate(-1));
            dto.setStartTime(firstDate(-7));
            dto.setStartTimeStr(DateFormatUtils.format(dto.getStartTime(),Patten.DATETIME));
            dto.setEndTimeStr(DateFormatUtils.format(dto.getEndTime(),Patten.DATETIME));
        } else if (QuickStartTimeE.Last30Days.v.equals(quickTime)) {
            dto.setEndTime(lastDate(-1));
            dto.setStartTime(firstDate(-30));
            dto.setStartTimeStr(DateFormatUtils.format(dto.getStartTime(),Patten.DATETIME));
            dto.setEndTimeStr(DateFormatUtils.format(dto.getEndTime(),Patten.DATETIME));
        }else{
            Date end = new Date();
            dto.setEndTime(end);
            dto.setStartTime(firstDate(0));
            dto.setStartTimeStr(DateFormatUtils.format(dto.getStartTime(),Patten.DATETIME));
            dto.setEndTimeStr(DateFormatUtils.format(dto.getEndTime(),Patten.DATETIME));
        }
        return dto;
    }

    public static Date firstDate(int plusday) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE,plusday);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date lastDate(int plusday) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE,plusday);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    public static Date plusDay(int plusday) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE,plusday);
        return calendar.getTime();
    }
    public static Date plusHour(int plusHour) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR,plusHour);
        return calendar.getTime();
    }

    public static void main(String[] args) {
        System.out.println(firstDate(0));
    }
}
